import type { ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import type { AddItem, AdminAccount, DetailItem, QueryListParams } from './data';
import { request } from 'umi';

type Pagination = { page?: number; pageSize?: number };

/**
 * 获取管理员列表
 * @param params
 */
export async function queryTaskList(
  params: Pagination & QueryListParams,
): Promise<RequestData<AdminAccount>> {
  const { page = 1, pageSize = 20, taskType, taskStatus } = params;
  const res: ResponsePaginationData<AdminAccount> = await request('/batch/task', {
    params: { page, pageSize, taskType, taskStatus },
    method: 'GET',
  });

  if (res?.code !== SUCCESS_CODE) {
    return { success: false, total: 0, data: [] };
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

export async function queryAdminDetail(params: {
  id: string;
}): Promise<{ success: boolean; data?: DetailItem }> {
  const res = await request(`/admin/admin/${params.id}`, {
    method: 'GET',
  });

  if (res?.code !== SUCCESS_CODE) {
    return { success: false };
  }

  return {
    success: true,
    data: res.data,
  };
}

/**
 * 创建一个管理员
 * @param params
 */
export async function createBatchTask(params: AddItem) {
  return request('/batch/task/create', {
    method: 'POST',
    data: { ...params, taskType: params.batchType },
  });
}
